var util = require('../../utils/util');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';
import {$wuxLoading} from '../../dist/index';

Page({
  data: {
    show:true,
    machine_code: [],
    error:false,
    note:"",
    focus: false
  },
  onLoad: function () {
	util.loading = $wuxLoading();
    this.setData({
      logs: (wx.getStorageSync('logs') || []).map(log => {
        return util.formatTime(new Date(log))
      })
    })
  },
  scann:function(){
      var that = this;
      var show ;
      var machine_code;
      wx.scanCode({
        success:(res) =>{
          this.machine_code = res.result;
          that.setData({
            show : false,
            machine_code : this.machine_code
          })
        },
        fail:(res) =>{
          that.setData({
            show: this.show
          })
        }
      })
    },
    noteing:function(e){
        this.data.note = e.detail.value.trim()
    },
    // 提交表单
    postData:function(){
      var that = this;
      var show;
      var error;
      var pattern = new RegExp("^[0-9]{6}$");
      if(!pattern.test(this.machine_code)){
          that.setData({
            error:"请扫描正确的二维码！"
          }
          );
          setTimeout(function(){
            that.setData({
                error:''
            })
          },2000);
          that.setData({
            show: true,
          });
          return false;
      };

      var url = config.addFeedBack;
      var params = {
        machineNo: this.machine_code,
        desc: this.data.note
      }
      wxRequest.postRequest(url, params) 
      .then(res=>{
          if(res==1){
              util.alert("上报成功",function(){
                  wx.navigateBack({ changed: true });
              })
          }
      })
      .catch(res => {
          util.alert_catch(res.message);
      })
      .finally(function (res) {
          util.hideLoading();
      });

    }
})
